﻿<?php
header("Access-Control-Allow-Origin: *");
//Header("content-type: application/x-javascript");
//apache_setenv('no-gzip', 1);
ini_set('zlib.output_compression', 0);
ini_set('implicit_flush', 1);
ini_set('allow_url_fopen', 'On');
ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');
set_time_limit(1000);

require_once 'dbconnector.php';

class WorkingHard {

	public static function main($strUrl) {		
		DbConnector::connectDB();
		
		$arrServidores = DbConnector::getServidores();
		$arrEncurtadores = DbConnector::getEncurtadores();
		//self::verifyHeader($strUrl);
		$arrMatches = self::findUrlPattern($strUrl, $arrEncurtadores, $arrServidores);
		
		//debug::x($arrMatches);
		
		$strUrl = $arrMatches[1][0];
		$strPatternUrl = $arrMatches[2][0];
		
		$strUrl = self::verifyHeader($strUrl);
		
		echo '<a href="'.$strUrl.'" target="_blank">'.$strUrl.'</a>';die();
		
		if(in_array($strPatternUrl, $arrEncurtadores))
			$strUrlFinal = self::findUrlFinal($strUrl, $arrServidores);
	}
	
	public static function findUrlFinal($strUrl, $arrServidores) {
		//$arrHeader = get_headers($strUrl);
		//echo '<pre>'; print_r($arrHeader); die();
		$strServidores = implode('|', $arrServidores);
		
		foreach($arrHeader as $strValue) {
			if(strstr($strValue, 'Location:'))
				$arrLocation[] = $strValue;
		}
		//echo '<pre>'; print_r($arrLocation); die();
		$intPosicoes = count($arrLocation);
		
		if($intPosicoes > 1) {
			$strURLDestino = $arrLocation[$intPosicoes - 1];
		} else {
			$strURLDestino = $arrLocation[0];
		}
		
		$arrPiecesLocation = explode(' ', $strURLDestino);
		//echo '<pre>'; print_r($arrPiecesLocation); die();
		
		//echo '<pre>'; print_r($arrPiecesLocation[1]);
		//echo '<pre>'; print_r($strUrl);
		
		$strERLocation = '#(http://('.$strServidores.')/[^ ]{1,100})#';
		
		preg_match_all($strERLocation, $strURLDestino, $arrURLDestinoFinal);	
		
		//echo '<pre>'; print_r($arrURLDestinoFinal);
			
		return $strURLDestino;	
	}
	
	public static function verifyHeader($strUrl) {
		$arrHeader = get_headers($strUrl);
		//debug::x($arrHeader);
		foreach($arrHeader as $strValue) {
			if(strstr($strValue, 'Refresh:'))
				$arrRefresh[] = $strValue;
			if(strstr($strValue, 'Location:'))
				$arrLocation[] = $strValue;
		}
		if(isset($arrRefresh)) {
			$arrRefresh = explode('=', $arrRefresh[0]);
			$strRefresh = str_replace(')', '', $arrRefresh[1]);
			//debug::x($strRefresh, 1);
			return $strRefresh;
		}	
		if(isset($arrLocation)) {
			$arrLocation = explode(' ', $arrLocation[0]);
			return $arrLocation[1];
		}		
		return $strUrl;
		
		//debug::x($arrLocation, 1);
	}
	
	public static function findUrlPattern($strUrl, $arrEncurtadores, $arrServidores) {
		$strUrlPattern = array_merge($arrEncurtadores, $arrServidores);		
		$strLinks = implode('|', $strUrlPattern);
		//echo '<pre>'; print_r($strLinks);
		if(strstr($strUrl, '//:ptth')) {
			foreach($arrEncurtadores as $strValue) {
				$arrEncurtadoresInvert[] = strrev($strValue);
			}
			//echo '<pre>'; print_r($arrEncurtadoresInvert); die();
			foreach($arrServidores as $strValue) {
				$arrServidoresInvert[] = strrev($strValue);
			}
			//echo '<pre>'; print_r($arrServidoresInvert); die();
			
			$arrLinksInvert = array_merge($arrEncurtadoresInvert, $arrServidoresInvert);
			
			$strLinksInvert = implode('|', $arrLinksInvert);
			
			//echo '<pre>Links Invertidos: '; print_r($strLinksInvert);
			
			$strUrl = strrev($strUrl);
			//echo '<pre>URL Invertida: '; print_r($strUrl);
			
			//$strER = '#(http://('.$strLinks.')(.*)('.$strLinksInvert.')//:ptth)#';
			$strER = '#(http://('.$strLinks.')(.*))/('.$strLinksInvert.')#';
			//echo '<pre>'; print_r($strER);
			preg_match_all($strER, $strUrl, $arrMatches);
			//echo '<pre>'; print_r($arrMatches); die();
			if(strstr($arrMatches[1][0], '=')) {
				$arrMatchesFixed = explode('=', $arrMatches[1][0]);
				$arrMatches[1][0] = $arrMatchesFixed[0];
			}
				
			
			return $arrMatches;
		} else {
			//echo '<pre>'; print_r($strLinks); die();
			$strER1 = '#(http://('.$strLinks.')/[^"<> ]{1,100})#';
			//echo '<pre>';print_r($strER1);
			preg_match_all($strER1, $strUrl, $arrMatches);
			//echo '<pre>'; print_r($arrMatches); die();
			$intLinksEncontrados = count($arrMatches[0]);
			
			if($intLinksEncontrados == 0) {
				echo 'URL not found.';
				die();
			}
			
			return $arrMatches;
		}
	}
	
	static function readFile() {
		$handle = fopen('links.txt', 'r');
		while(!feof($handle)) 
			echo fgets($handle);
	}
	
}

class debug {
	public static function x($strValor, $boolExit = 1) {
		echo '<pre>';
		print_r($strValor);
		if($boolExit)
			exit();
		echo '</pre>';
	}
}
if(isset($_GET['url']))
	WorkingHard::main($_GET['url']);

?>